﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Diagnostics;

namespace WindowsPhoneAppHost
{
    public partial class TestDetail_2 : PhoneApplicationPage
    {
        public TestDetail_2()
        {
            InitializeComponent();
            DoHttpWebRequest();
            //test();
        }

        private void test()
        {
            listBox1.Items.Add(new ListSeperator());
            for (int i = 0; i < 10; i++)
            {
                RemarkItem content = new RemarkItem();
                listBox1.Items.Add(content);
                listBox1.Items.Add(new ListSeperator());
            }
        }
        //*
        private void DoHttpWebRequest()
        {
            string url = "http://localhost/MobileOA/Remarks.json";
            WebRequest request = WebRequest.Create(url); //创建WebRequest类
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request); //返回异步操作的状态
        }

        private void ResponseCallback(IAsyncResult result)
        {
            var request = (HttpWebRequest)result.AsyncState; //获取异步操作返回的的信息
            WebResponse response = request.EndGetResponse(result); //结束对 Internet 资源的异步请求
            using (Stream stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string contents = reader.ReadToEnd();
                //Dispatcher.BeginInvoke(delegate() { this.textBlock1.Text = contents; });
                Dispatcher.BeginInvoke(delegate { ParseJson(contents); });
            }
        }

        //JObject.TryGetValue ??
        private string getJsonString(JObject obj,string name)
        {
            if (null != obj[name])
            {
                return obj[name].ToString();
            }
            return "";
        }

        private void ParseJson(string jsonString)
        {
            Debug.WriteLine(jsonString);
            JArray jArray = JArray.Parse(jsonString);
            Debug.WriteLine(jArray.Count.ToString());
            listBox1.Items.Add(new ListSeperator());
            for (int i = 0; i < jArray.Count; i++)
            {
                var obj = (JObject)jArray[i];
                RemarkItem item = new RemarkItem();
                item.Title.Text = obj["title"].ToString();
                item.Type.Text = obj["type"].ToString();
                item.Comment.Text = obj["comment"].ToString();
                item.Author.Text = obj["author"].ToString();
                item.Dept.Text = obj["dept"].ToString();
                item.Time.Text = obj["time"].ToString();
                listBox1.Items.Add(item);
                listBox1.Items.Add(new ListSeperator());
            }
        }
         //*/
    }
}